package com.saber.accumulator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.saber.bean.Report;
import com.saber.bean.ReportItem;


/**
 *
 * @author
 *
 */
public class Avg implements ReportItemAccumulator {

	/**
	 *
	 */
	private ReportItem.AccumulatorType[] supported = new ReportItem.AccumulatorType[] {ReportItem.AccumulatorType.AVG};

	@Override
	public ReportItem.AccumulatorType[] getSupported() {
		return supported;
	}

	@Override
	public void accumulate(Report report, ReportItem reportItem, Object value) {
		JSONObject jvalue = (JSONObject)value;
		JSONObject json = JSON.parseObject(reportItem.getValue());
		json.put("count", json.getLong("count") + jvalue.getLong("count"));
		json.put("value", json.getLong("value") + jvalue.getLong("value"));
		reportItem.setValue(json.toJSONString());
	}

}
